/*
 * @Author         : jiabao
 * @Description    :
 * @Date           : 2024-07-17 20:19:21
 * @FilePath       : \demo_cc\base\src\base_sys.c
 */
#include "base_sys.h"

s32 base_sys_cls()
{
#ifdef _WIN32
    system("cls");
#elif __linux__
    system("clear");
#endif
    return BASE_OK;
}

s32 base_sys_pause()
{
#ifdef _WIN32
    system("pause");
#elif __linux__
    system("pause");
#endif
    return BASE_OK;
}

#ifdef _WIN32
s32 base_nar2wide(u08 *nar, wchar_t *wide)
{
    if (nar == NULL || wide == NULL)
    {
        printf("base_nar2wide parm is error\n");
        return BASE_ERROR;
    }
    // 需要多分配一个空间，以存放终止符
    return mbstowcs(wide, nar, strlen(nar) + 1);
}
#endif

ull base_get_total_space(u08 *path)
{
    ull totalS = 0;
#ifdef _WIN32
    ULARGE_INTEGER totalNumberOfBytes;
    wchar_t *widePath = (wchar_t *)malloc((strlen(path) + 1) * sizeof(wchar_t));
    base_nar2wide(path, widePath);
    if (!GetDiskFreeSpaceExW(widePath, NULL, &totalNumberOfBytes, NULL))
    {
        if (widePath != NULL)
        {
            free(widePath);
            widePath = NULL;
        }
        printf("GetDiskFreeSpaceEx failed with error:%d\n", GetLastError());
        return totalS;
    }
    if (widePath != NULL)
    {
        free(widePath);
        widePath = NULL;
    }
    totalS = totalNumberOfBytes.QuadPart;
#elif __linux__

#endif
}
ull base_get_free_space(u08 *path)
{
    ull freeS = 0;
#ifdef _WIN32
    ULARGE_INTEGER freeNumberOfBytes;
    wchar_t *widePath = (wchar_t *)malloc((strlen(path) + 1) * sizeof(wchar_t));
    base_nar2wide(path, widePath);
    if (!GetDiskFreeSpaceExW(widePath, &freeNumberOfBytes, NULL, NULL))
    {
        if (widePath != NULL)
        {
            free(widePath);
            widePath = NULL;
        }
        printf("base_get_free_space failed with error:%d\n", GetLastError());
        return freeS;
    }
    if (widePath != NULL)
    {
        free(widePath);
        widePath = NULL;
    }

    freeS = freeNumberOfBytes.QuadPart;
#elif __linux__

#endif
}
ull base_get_use_space(u08 *path)
{
    return base_get_total_space(path) - base_get_free_space(path);
}

s32 base_sys_test()
{
#ifdef _WIN32
    u08 *path = "C:\\";
#elif __linux__
    u08 *path = "/";
#endif
    printf("%s total space:%lld MB\n", path, base_get_total_space(path) / 1024 / 1024);
    printf("%s free  space:%lld MB\n", path, base_get_free_space(path) / 1024 / 1024);
    printf("%s use   space:%lld MB\n", path, base_get_use_space(path) / 1024 / 1024);

    return BASE_OK;
}
